<?php

namespace Repository;

use Doctrine\ORM\EntityRepository;

class UserinfoRepository extends EntityRepository {
	public function backendPaginate($conf){
		$q = $this->_em->getRepository('Entity\ShUserinfo')->findBy(
			$conf['condition'],$conf['order'],$conf['limit'],$conf['offset']
			);
		return $q;
	}
	public function total_rows($pag){
		$q = $this->_em->createQueryBuilder()
		->select('COUNT(uf.id)')
		->from('Entity\ShUserinfo','uf');
		return $q->getQuery()->getSingleScalarResult();
	}
	public function addUserinfo($data){
		$uf = new \Entity\ShUserinfo();

		$u = $this->_em->getRepository('Entity\ShUser')->findOneById($data->post('id-user'));
		if(empty($u)):
			return false;
		else:
		$userinfo->setFirstname($data->post('ho'));
		$userinfo->setLastname($data->post('ten'));
		$userinfo->setGender($data->post('gioi-tinh'));
		$userinfo->setAddress($data->post('dia-chi'));
		$userinfo->setEmail($data->post('email'));
		$userinfo->setBirthday(time());
		$userinfo->setDate(time());
		$userinfo->setShUser($u);

		$this->_em->persist($userinfo);
		$this->_em->flush();
		return true;
		endif;
	}
	public function updateUserinfo($data, \Entity\ShUserinfo $userinfo){
		$u = $this->_em->getRepository('Entity\ShUser')->findOneById($data->post('id-user'));

		if(empty($u)):
			return false;
		else:
		$userinfo->setFirstname($data->post('ho'));
		$userinfo->setLastname($data->post('ten'));
		$userinfo->setGender($data->post('gioi-tinh'));
		$userinfo->setAddress($data->post('dia-chi'));
		$userinfo->setEmail($data->post('email'));
		$userinfo->setBirthday(time());
		$userinfo->setDate(time());
		$userinfo->setShUser($u);

		$this->_em->persist($userinfo);
		$this->_em->flush();
		return true;
		endif;
	}
	public function deleteUserinfo($userinfo)
	{
		$data = $this->_em->getRepository('Entity\ShUserinfo')->findOneById($userinfo);
		$this->_em->remove($data);
		$this->_em->flush();
		return true;
	}
	
}